package com.chuang.bootplus.base.utils;

import com.chuang.bootplus.base.exception.BusException;
import com.chuang.bootplus.config.email.EmailConfig;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description: TODO
 * @author nuo
 * @date 2022/8/18 2:43
 * @version 1.0
 */
@Component
public class EmailUtil {

    @Resource
    private EmailConfig mailConfig;
    @Resource
    private JavaMailSender javaMailSender;

    public ApiResponse<String> sendMsg(String email, String subject, String msg) {
        try {
            //消息构造器
            SimpleMailMessage message = new SimpleMailMessage();
            //发件人
            message.setFrom(mailConfig.getUsername());
            //收件人
            message.setTo(email);
            //主题
            message.setSubject(subject);
            //正文
            message.setText(msg);

            javaMailSender.send(message);

            return new ApiResponse<>("发送成功...");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusException("发送失败...");
        }

    }

}
